package com.resource.exammanage.config;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeJudgeUtils {

    public static String getNowDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }
    //起始时间和截止时间比较,如果为ture表示到达了截止时间
    public static boolean judgeTime(String startTime,String endTime){
        boolean timeJudge = false;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            Date startDate=df.parse(startTime);
            Date endDate=df.parse(endTime);
            if(startDate.getTime()>endDate.getTime()){
                timeJudge=true;
            }else{
                timeJudge=false;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return timeJudge;
    }
    //给定一个截止时间,默认当前时间为起始时间
    public static boolean judgeIsEndTime(String endTime){
        boolean timeJudge = false;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            Date startDate=df.parse(getNowDate());
            Date endDate=df.parse(endTime);
            if(startDate.getTime()>endDate.getTime()){
                timeJudge=true;
            }else{
                timeJudge=false;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return timeJudge;
    }

    //返回指定时间加指定天后的结果时间
    public static  String addDayTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(startDate));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.DATE,n);
        return  sdf.format(cd.getTime());
    }
    //返回指定时间加指定月后的结果时间
    public static  String addMonthTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(startDate));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.MONTH,n);
        return  sdf.format(cd.getTime());
    }
    //返回指定时间加指定年后的结果时间
    public static  String addYearTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(startDate));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.YEAR,n);
        return  sdf.format(cd.getTime());
    }
    //返回指定时间加指定小时后的结果时间
    public static  String addHourTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(startDate));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.HOUR_OF_DAY,n);
        return  sdf.format(cd.getTime());
    }
    //返回指定时间加指定分钟后的结果时间
    public static  Date addMinuteTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(startDate));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.MINUTE,n);
        return  cd.getTime();
    }
    //返回指定时间加指定秒后的结果时间
    public static  String addSecondTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(startDate));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.SECOND,n);
        return  sdf.format(cd.getTime());
    }

    //返回当前时间加指定天后的结果时间
    public static  String addNowDayTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(getNowDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.DATE,n);
        return  sdf.format(cd.getTime());
    }
    //返回当前时间加指定月后的结果时间
    public static  String addNowMonthTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(getNowDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.MONTH,n);
        return  sdf.format(cd.getTime());
    }
    //返回当前时间加指定年后的结果时间
    public static  String addNowYearTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(getNowDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.YEAR,n);
        return  sdf.format(cd.getTime());
    }
    //返回当前时间加指定小时后的结果时间
    public static  String addNowHourTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(getNowDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.HOUR_OF_DAY,n);
        return  sdf.format(cd.getTime());
    }
    //返回当前时间加指定分钟后的结果时间
    public static  String addNowMinuteTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(getNowDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.MINUTE,n);
        return  sdf.format(cd.getTime());
    }
    //返回当前时间加指定秒后的结果时间
    public static  String addNowSecondTime(String startDate,int n){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd=Calendar.getInstance();//获取一个Calendar对象
        try{
            cd.setTime(sdf.parse(getNowDate()));
        }catch (Exception e){
            e.printStackTrace();
        }
        cd.add(Calendar.SECOND,n);
        return  sdf.format(cd.getTime());
    }

}


